/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.uca.sisbec.actions.manto;

import com.uca.sisbec.commons.HibernateAction;
import com.uca.sisbec.forms.manto.MateriaForm;
import com.uca.sisbec.forms.manto.RolForm;
import com.uca.sisbec.helpers.manto.MateriaHelper;
import com.uca.sisbec.mapping.Becario;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.*;
import org.hibernate.Transaction;

/**
 *
 * @author Susy
 */
public class MateriaAction extends HibernateAction {

    public ActionForward init(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        MateriaForm mf = (MateriaForm) form;
        mf.setCarreras(new int[0]);
        mf.setDisponibles(new String[0]);
        mf.setSeleccionadas(new String[0]);
        setOptionsList(request, mf);
        request.setAttribute("save", "new");
        return mapping.findForward("success");
    }

    public ActionForward guardarMateria(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        MateriaForm mf = (MateriaForm) form;
        MateriaHelper mh = new MateriaHelper(getSession(request));
        ActionMessages msg = new ActionMessages();
        ActionErrors errors = new ActionErrors();
        if (!mh.existeCodMateria(mf)) {
            mh.guardarMateriaDB(mf);
            mf.setCarreras(new int[0]);
            mf.setDisponibles(new String[0]);
            mf.setSeleccionadas(new String[0]);
            setOptionsList(request, mf);
            msg.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("materia.nueva"));
            request.setAttribute("msg", msg);
        } else {
            errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("materia.existe", mf.getCodigo()));
            request.setAttribute("errors", errors);
            mf.setCarreras(new int[0]);
            mf.setDisponibles(new String[0]);
            mf.setSeleccionadas(new String[0]);
            setOptionsList(request, mf);
        }
        request.setAttribute("save", "new");
        return mapping.findForward("success");
    }

    private void setOptionsList(HttpServletRequest request, MateriaForm mf) {
        MateriaHelper mh = new MateriaHelper(getSession(request));
        request.setAttribute("UniversidadesList", mh.getAllUniversidades());
        request.setAttribute("availableCarrerasList", mh.getAvailableCarreras(mf.getIdUniv()));
        request.setAttribute("availableList", mh.getAvailableMaterias(mf.getCodCarrera()));
        request.setAttribute("selectedList", mh.getSelectedMaterias(mf.getCodCarrera()));
    }

    public ActionForward agregarMateriaCarrera(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        MateriaForm mf = (MateriaForm) form;
        MateriaHelper mh = new MateriaHelper(getSession(request));
        mh.eliminarTodo(mf);
        getTx(request).commit();
        Transaction tx = getSession(request).beginTransaction();
        request.getSession().setAttribute("_TRX_HIB", tx);
        
        mh.agregarMateriasCarrera(mf);
        mf.setCarreras(new int[0]);
        mf.setDisponibles(new String[0]);
        mf.setSeleccionadas(new String[0]);
        setOptionsList(request, mf);
        request.setAttribute("save", "new");
        return mapping.findForward("success");

    }
    
    public ActionForward seleccionarMateria(ActionMapping mapping, ActionForm form, 
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        System.out.println("Entre a Seleccionar Materia");
        MateriaForm mf = (MateriaForm) form;
        MateriaHelper helper = new MateriaHelper(getSession(request));
        helper.setMateriaForm(mf);
        mf.setCarreras(new int[0]);
        mf.setDisponibles(new String[0]);
        mf.setSeleccionadas(new String[0]);
        setOptionsList(request, mf);
        request.setAttribute("save", "update");
        return mapping.findForward("success");
    }
    
    public ActionForward modificarMateria(ActionMapping mapping, ActionForm form, 
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        ActionMessages msg = new ActionMessages();
        ActionErrors errors = new ActionErrors();
        MateriaForm mf = (MateriaForm) form;
        MateriaHelper mh = new MateriaHelper(getSession(request));
        mh.modificarMateria(mf);
        mf.setCarreras(new int[0]);
        mf.setDisponibles(new String[0]);
        mf.setSeleccionadas(new String[0]);
        setOptionsList(request, mf);
        msg.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("materia.modificada"));
        request.setAttribute("msg", msg);
        request.setAttribute("save", "update");
        return mapping.findForward("success");
    }
    
    public ActionForward searchMateria(ActionMapping mapping, ActionForm form, 
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        MateriaForm mf = (MateriaForm) form;
        mf.setEstado(true);
        MateriaHelper helper = new MateriaHelper(getSession(request));
        request.setAttribute("CarrerasList", helper.getAllCarreras(mf));
        request.setAttribute("UniversidadesList", helper.getUniversidadesActivas());
        request.setAttribute("MateriasList",helper.getAllMaterias(mf));
        return mapping.findForward("add");
    }
    
    public ActionForward InhabilitarMateria(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        MateriaForm mf = (MateriaForm) form;
        ActionMessages msg = new ActionMessages();
        Becario b = (Becario) request.getSession().getAttribute(_BECARIO);
        MateriaHelper mh = new MateriaHelper(getSession(request));
        mh.InhabilitarMateriaDB(request.getParameter("cod"));
        getSession(request).flush();
        request.setAttribute("MateriasList", mh.obtenerMateriasXCarrera(mf,b));
        request.setAttribute("load","carga");
        msg.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("materia.habilitada"));
        request.setAttribute("msg", msg);
        return mapping.findForward("success");
    }
    
    public ActionForward HabilitarMateria(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        ActionMessages msg = new ActionMessages();
        MateriaForm mf = (MateriaForm) form;
        Becario b = (Becario) request.getSession().getAttribute(_BECARIO);
        MateriaHelper mh = new MateriaHelper(getSession(request));
        mh.HabilitarMateriaDB(request.getParameter("cod"));
        getSession(request).flush();
        request.setAttribute("CarrerasList", mh.getAllCarreras(mf));
        request.setAttribute("UniversidadesList", mh.getUniversidadesActivas());
        request.setAttribute("MateriasList", mh.obtenerMateriasXCarrera(mf,b));
        request.setAttribute("load","carga");
        msg.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("materia.habilitada"));
        request.setAttribute("msg", msg);
        return mapping.findForward("add");
    } 
    
}
